package com.itheima.service.impl;

import com.itheima.mapper.ReportMapper;
import com.itheima.service.ReportService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Map totalMemberCounts() {
        List<Map> mapList =  reportMapper.totalMemberCounts();
        return  changeMapListTOMap(mapList);
    }

    @Override
    public List<Map> orderSetmealProportion() {

       return reportMapper.orderSetmealProportion();
    }

    private Map changeMapListTOMap(List<Map> mapList) {
        Map  map = new HashMap();
        List<String> yearAndMonths = new ArrayList<>();
        List<Long> memberCounts = new ArrayList<>();
        for (Map map1 : mapList) {
            memberCounts.add((Long)map1.get("totalCounts"));
            yearAndMonths.add((String)map1.get("yearAndMonth"));
        }
        map.put("yearAndMonths",yearAndMonths);
        map.put("totalCounts",memberCounts);
        return  map;
    }
}
